function canTuan(ev, ok) {
	ev.stopPropagation();
	ok = (typeof ok != 'undefined') ? ok : false;
	if (ok) return true;
	$.blockUI({ message: $('#question'), css: { width: '400px',height:'100px',border:'10px solid #999999'} }); 
	return false;
}

$(document).ready(function() {
        $.ajax({
                type: "POST",
                url: "/stores/time_out",
                data: {
                tuan_id: $("#tuan_id").val(),
                type: "get"},
                success: function(time_out)
                {
			if (time_out != '0') {
				var date = new Date();  
				var parts = String(time_out).split(/[- : T]/);  

				date.setFullYear(parts[0]);  
				date.setMonth(parts[1] - 1);  
				date.setDate(parts[2]);  
				date.setHours(parts[3]);  
				date.setMinutes(parts[4]);  
				date.setSeconds(parts[5]);  
				date.setMilliseconds(0);  
				$('#compact').countdown({until:date, compact:true,description:''});
			}
                }
        });

	var items_on_page = 10;
	var pages_step = 5;

        get_data_comments(1, 10);
        $.ajax({
                type: "POST",
                url: "/stores/comments_total",
                data: {
                store_id: $("#store_id").val(),
                type: "get"},
                success: function(total)
                {
                        $("#paging_comments").jpaging({
                                all_items_num: total,
                                callback: get_data_comments,
                                items_on_page: items_on_page,
                                pages_step: pages_step
                        });
                }
        });

	function get_data_comments(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/stores/comments_paging",
			data: {start_index: start_index,
			end_index: end_index,
			store_id: $("#store_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_comments").html(html);
				$.prettyDate.now = function() {
					return new Date();
				}
                                $(".prettyDate").prettyDate();

			}
		});
        }


	$("#accept_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/stores/accept_proposal",
			data: {store_id: $("#store_id").val(),
			tuan_id: $("#tuan_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#to_be_processed_div").html(html);
			}
		});
        });

	$("#accept_extend_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/stores/accept_extend_button",
			data: {store_id: $("#store_id").val(),
			tuan_id: $("#tuan_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#to_be_processed_div").html(html);
			}
		});
        });

	$("#refuse_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/stores/refuse_button",
			data: {store_id: $("#store_id").val(),
			tuan_id: $("#tuan_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#to_be_processed_div").html(html);
			}
		});
        });

	$("#bargin_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/stores/bargin_button",
			data: {store_id: $("#store_id").val(),
			tuan_id: $("#tuan_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#to_be_processed_div").html(html);
			}
		});
        });

	$("#bargin_extend_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/stores/bargin_extend_button",
			data: {store_id: $("#store_id").val(),
			tuan_id: $("#tuan_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#to_be_processed_div").html(html);
			}
		});
        });

	$("#post_comment_button").click(function(){
                var comment = $("#store_comment_text").val();
		if(comment.length > 0) {
			$("#store_comment_text").attr("value", '');
			comment = comment.replace('\n', '<br />');
			$.ajax({
				type: "POST",
				url: "/stores/comment",
				data: {
				comm_id: $("#comm_id").val(),
				store_id: $("#store_id").val(),
				comment: comment,
				type: "get"},
				success: function(html)
				{
					get_data_comments(1, 10);
					$.ajax({
						type: "POST",
						url: "/stores/comments_total",
						data: {
						store_id: $("#store_id").val(),
						type: "get"},
						success: function(total)
						{
							$("#paging_comments").jpaging({
								all_items_num: total,
								callback: get_data_comments,
								items_on_page: items_on_page,
								pages_step: pages_step
							});
						}
					});

				}
			});
		}
        });

	$("#join_tuan_button").click(function(){
                $.ajax({
                        type: "POST",
                        url: "/tuans/join_tuan",
                        data: {tuan_id: $("#ongoing_tuanid").val(),
                        type: "get"},
                        success: function(html)
                        {
                                $("#join_tuan_div").html(html);
                        }
                });
        });

	$('#cantuan').bind('submit', canTuan);

	$('#yes').click(function() { 
		$('#cantuan').trigger('submit', [true]); 
		$.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</h1>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
	}); 

	$('#no').click(function() { 
		$.unblockUI(); 
		return false; 
		}); 
	
});
